<template>
	<view class="wrap">
		<js-error mode="bottom" ref="jsError"></js-error>
		<view class="action bg-white "
			style="padding: 15px 0 0 10px;font-size: 20px;align-items: center;display: flex;">
			<u-icon name="/static/image/invcode.png" size="80"></u-icon>
			<text class="cuIcon-titles text-bold">到货信息</text>
		</view>

		<u-form class="form bg-white" :model="model" ref="uForm" label-position="left">
			<u-form-item label="车次:" prop="djNo" label-width="200" :label-style="{'font-weight':'bold'}">
				<u-input placeholder=" " v-model="model.djNo" type="text" :disabled="true" di maxlength="64"></u-input>
			</u-form-item>
			<u-form-item label="车牌号:" prop="carNo" label-width="200" required :label-style="{'font-weight':'bold'}">
				<!-- <u-input placeholder="请输入" v-model="model.carNo" type="text" di maxlength="64"></u-input> -->
				<view style="display: flex;">
					<xm-cell style="flex: 1;" special label=" " :value="model.carNo" @show="showKeyboard('xmKeyboard')"></xm-cell>
					<view class="">
						<button class="cu-btn  bg-confirm " @click="paste">粘贴</button>
					</view>
				</view>
				
				<!-- 虚拟键盘载体 -->
				<xm-keyboard-v2 ref="xmKeyboard" @confirm="carNoconfirm"></xm-keyboard-v2>
			</u-form-item>
			<u-form-item label="计划日期" prop="time" label-width="200" required :label-style="{'font-weight':'bold'}">
				<u-input placeholder="请输入" v-model="model.planDate" type="select" :select-open="pickerTime"
					@click="pickerTime = true"></u-input>
			</u-form-item>
			<u-picker mode="time" v-model="pickerTime" @confirm="timeConfirm" :params="params"></u-picker>
			<u-form-item label="司机:" prop="cdriver" label-width="200" :label-style="{'font-weight':'bold'}">
				<u-input placeholder="请输入" v-model="model.cdriver" type="text" di maxlength="64"></u-input>
			</u-form-item>
			<!-- <u-form-item label="押运员:" prop="yyPerson" label-width="200" :label-style="{'font-weight':'bold'}">
				<u-input placeholder="请输入" v-model="model.yyPerson" type="text" di maxlength="64"></u-input>
			</u-form-item> -->
			<u-form-item label="司机电话:" prop="driverPhone" label-width="200" :label-style="{'font-weight':'bold'}">
				<u-input placeholder="请输入" v-model="model.driverPhone" type="text" di maxlength="64"></u-input>
			</u-form-item>
			<!-- <u-form-item label="押运电话:" prop="yyPhone" label-width="200" :label-style="{'font-weight':'bold'}">
				<u-input placeholder="请输入" v-model="model.yyPhone" type="text" di maxlength="64"></u-input>
			</u-form-item> -->
			<u-form-item label="运输单位:" prop="testCarVen" label-width="200" required
				:label-style="{'font-weight':'bold'}">
				<!-- <js-select v-model="model.carVenCode" :showFilter="true" :items="carVenSelectList" placeholder="请选择" :tree="true"
					:label-value="model.carVenName" @label-input="model.carVenName = $event"></js-select> -->
					<u-input placeholder="请选择" v-model="model.carVenName" type="select" 
						@click="carVenXz"></u-input>
			</u-form-item>
			<u-form-item label="备注:" prop="remarks" label-width="200" :label-style="{'font-weight':'bold'}">
				<u-input placeholder="请输入" v-model="model.remarks" type="text" di maxlength="64"></u-input>
			</u-form-item>
		</u-form>
		<view class="action bg-white "
			style="font-size: 20px;align-items: center;display: flex;padding: 0 0 10px 10px;">
			<u-icon name="/static/image/detail.png" size="80"></u-icon>
			<text class="cuIcon-titles text-bold">到货明细</text>
		</view>
		<view style="padding: 10px;">
			<view v-for="(item,index) in children" class="cu-item shadow " style="position: relative;" :key="index">
				<view class="cu-form-group" style="display: flex;justify-content: space-between;">
					<view
						style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
						{{ index + 1 }}
					</view>
					<view style="display: flex;align-items: center;">
							<u-icon name="/static/image/edit.png" @click="editDetail(item)"   size="70"></u-icon>
						
							<text style="font-size: 35px;margin-left: 20px;" class="cuIcon-deletefill text-sl text-red"
								@tap="delDetail(item.id)"></text>
					</view>
				</view>
				<view class="cu-form-group text-bold text-red">
					<view class="title">订单号：</view>
					<view style="flex: 1;"> {{ item.poCode|| ""  }}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">供应商：</view>
					<view style="flex: 1;"> {{ item.venName|| ""  }}
					</view>
				</view>
				<view class="cu-form-group">
					<view class="title">存货名称：</view>
					<view style="flex: 1;"> {{ item.invName|| ""  }} </view>
				</view>
				<view class="cu-form-group">
					<view class="title">规格型号：</view>
					<view style="flex: 1;"> {{ item.invStd|| ""  }} </view>
				</view>
				<view class="cu-form-group">
					<view class="title">合同号：</view>
					<view style="flex: 1;"> {{ item.contractCode|| ""  }} </view>
				</view>
				<view class="cu-form-group">
					<view class="title">计划重量：</view>
					<view style="flex: 1;"> {{ item.planWeight|| ""  }} </view>
				</view>
				<view class="cu-form-group">
					<view class="title">确认重量：</view>
					<view style="flex: 1;"> {{ item.qrWeight|| ""  }} </view>
				</view>
			</view>

	</view>
	<!-- <tui-card v-for="(item,index) in children" :key="index"
			:style="{'margin-bottom': '30rpx','margin-top':index===0?'30rpx':'0rpx'}">
			<template v-slot:body>
				<tui-list-cell :arrow="false" :hover="false">
					<view class="tui-item-box">
						<view class="tui-list-cell-name" style="position: relative;"><text
								style="color: #ff5500;font-weight: 1000;" class="text-bold">【{{index+1}}】</text><text
								class="text-bold">发货单号：{{item.soCode}}</text>
							<u-icon name="/static/common/img/delData.png" @click="delDetail(item.id)" color="#2979ff"
								class="rights" style="" size="30"></u-icon>
						</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :arrow="false" :hover="false">
					<view class="tui-item-box padding-left-lg">
						<view class="tui-list-cell-name">客户：{{item.cusName}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :arrow="false" :hover="false">
					<view class="tui-item-box padding-left-lg">
						<view class="tui-list-cell-name">存货名称：{{item.invName}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :arrow="false" :hover="false">
					<view class="tui-item-box padding-left-lg">
						<view class="tui-list-cell-name">规格型号：{{item.invStd}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :arrow="false" :hover="false">
					<view class="tui-item-box padding-left-lg">
						<view class="tui-list-cell-name">合同号：{{item.contractCode}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :arrow="false" :hover="false">
					<view class="tui-item-box padding-left-lg">
						<view class="tui-list-cell-name">计划发货重量：{{item.planWeight}}</view>
					</view>
				</tui-list-cell>
			</template>
		</tui-card> -->
	<view class="form-footer" @click="GoaddChild()">
		<view class="btn-plus">
			<u-icon name="plus-circle" size="90" color="#3E97B0"></u-icon>
		</view>
	</view>
	<view style="height: 70px;"></view>
	<!-- <view class="footer">
			<view class="confrim-btn padding padding-bottom-sm flex bg-white">
				<u-button type="primary" class="btn" @click="submit">保存</u-button>
				<u-button class="btn" type="default" @click="cancel">关闭</u-button>
			</view>
		</view> -->
	<view class=" flex cu-bar tabbar bg-white foot cu-modal-footer">
		<button class="cu-btn  bg-confirm lg  " @click="submit">保存</button>
		<button class="cu-btn  lines-red lg margin-left" @click="cancel">关闭</button>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				model: {
					// id:'',
					// carNo:'',
					// planDate:'',
					// cdriver:'',
					// driverPhone:'',
					// carVenCode:'',
					// carVenName:'',
					// remarks:'',
				},
				//主表id
				query: {
					id: '',
				},
				//子表id
				querys: {
					id: '',
				},
				children: [],
				carVenSelectList: [],
				pickerTime: false, //控制日期显示
			}
		},
		onLoad(params) {
			
			if(params.id){
				this.query.id = params.id
				this.loadList();
			}
			
			
			uni.$on('refreshData', () => {
				this.loadList();
			})
			
			uni.$on('carVenData', (data) => {
				console.log(data,'data====');
				this.model.carVenCode = data.id 
				this.model.carVenName = data.name
				this.$forceUpdate()
			})
		},
		onShow() {},
		onReady() {
			// 运输单位数据
			this.$u.api.carVen.treeData().then(res => {
				this.carVenSelectList = res;
			});
		},
		methods: {
			paste(){
				let that = this
				uni.getClipboardData({
					success: (res) => {  // 获取成功回调

						that.model.carNo = res.data.trim().slice(0,8)
						that.$forceUpdate()
						if(!that.model.cdriver && !that.model.driverPhone){
							that.$u.api.mfsh.getCarInfoByCarNo({carNo:that.model.carNo}).then(res => {
								that.model.cdriver = res.data.cdriver || '';
								that.model.driverPhone = res.data.driverPhone || '';
								that.$forceUpdate()
							});
						}
					}
				})
			},
			carNoconfirm(v){
				this.model.carNo = v;
				this.$forceUpdate()
				if(!this.model.cdriver && !this.model.driverPhone){
					this.$u.api.mfsh.getCarInfoByCarNo({carNo:v}).then(res => {
						this.model.cdriver = res.data.cdriver || '';
						this.model.driverPhone = res.data.driverPhone || '';
						this.$forceUpdate()
					});
				}
			},
			showKeyboard(ref){
				this.$refs[ref].toShow(this.model.carNo)
			},
			cancel() {
				// uni.$emit('refreshData');
				uni.navigateBack({
					delta: 1,
				})
			},
			toFindDetail(model) {
				const that = this
				uni.navigateTo({
					url: '/pages/mf/fh/fhDetailList?hid=' + model.id,
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {

						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						// res.eventChannel.emit('initfilter', that.filter)
					}
				})
			},
			toAddDetail(model) {
				const that = this
				uni.navigateTo({
					url: '/pages/mf/fh/fhHwAdd?hid=' + model.id,
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {

						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						// res.eventChannel.emit('initfilter', that.filter)
					}
				})
			},
			delDetail(id) {
				this.querys.id = id;
				uni.showModal({
					title: '删除提示',
					content: '是否删除此详情？',
					confirmColor: '#F54E40',
					success: (res) => {
						if (!res.confirm) {
							this.isdisabled = true
							return false;
						}
						this.$u.api.mfsh.delDetail(this.querys).then(res => {
							if (res.result=="true") {
								this.$u.toast("删除成功！");
							} else {
								this.$refs.jsError.showError('',res.message,'error');
							}
							this.querys.id = ''
							this.loadList();
						})
					}
				})
			},
			editDetail(model){
				const that=this
				uni.navigateTo({
				  url: '/pages/mf/dh/dhEdit?model='+JSON.stringify(model),
				  events: {
				    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				    Filter(data) {
						
				    }
				  },
				  success: function(res) {
				    // 通过eventChannel向被打开页面传送数据
				    // res.eventChannel.emit('initfilter', that.filter)
				  }
				})
			},
			loadList() {
				this.$u.api.mfsh.findList(this.query).then(res => {
					if (!res.list || res.list.length == 0) {
						return;
					}
					var data = res.list[0];
					this.model = data;
					this.children = data.mfCarplanDhHList;
				})
			},
			carVenXz(){
				let obj = {}
				if(this.model.carVenName){
					obj = {
						id:this.model.carVenCode,
						name:this.model.carVenName,
					}
				}
				const that = this;
				uni.navigateTo({
					url: '/components/treeList/list?item=' + JSON.stringify(obj),
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {
							console.log(data)
						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						//res.eventChannel.emit('initfilter', that.queryData)
					}
				})
			},
			GoaddChild() {
				if (this.model.id == null || this.model.id == '') {
					this.$refs.jsError.showError('','请先保存到货信息！','error');
					return;
				} else {
					const that = this;
					uni.navigateTo({
						url: '/pages/mf/dh/poList?hid=' + this.model.id + '&carType='+ this.model.carType,
						events: {
							// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
							Filter(data) {
								console.log(data)
							}
						},
						success: function(res) {
							// 通过eventChannel向被打开页面传送数据
							//res.eventChannel.emit('initfilter', that.queryData)
						}
					})
				}
			},
			timeConfirm(e) {
				this.model.planDate = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ":" + e.minute + ":" + e
					.second;
			},
			submit(data, callback) {
				if(this.model.carNo == null || this.model.carNo == ''){
					this.$refs.jsError.showError('','请正确填入车牌号！','error');
					return;
				}else if(this.model.planDate == null || this.model.planDate == ''){
					this.$refs.jsError.showError('','请正确选择计划到货日期！','error');
					return;
				}else if(this.model.carVenCode == null || this.model.carVenCode == ''){
					this.$refs.jsError.showError('','请正确选择运输单位！','error');

					return;
				}else{
					var data = {
					mfCarplanDhH: JSON.stringify(this.model),
					useStatus: 1
					};
					this.$u.api.mfsh.save(data).then(res => {
						if(res.result == 'true'){
							this.$u.toast(res.message)
							this.model=res.data;
							this.query.id=this.model.id;			
						}else{
							this.$refs.jsError.showError('',res.message,'error');
						}
					});
				}
			},
		}
	}
</script>
<style lang="less">
	/* .footer {
	position: fixed;
	bottom: 0;
	right:0;
	width: 100%;
	line-height: var(--footer-height);
	background: #ffffff;
	color: #ffffff;
} */
	.btn-plus {
		position: fixed;
		bottom: 260rpx;
		right: 40rpx;
		z-index: 2;
		opacity: 0.6;
	}

	.btn-plus:hover {
		opacity: 1;
	}

	.add {
		position: absolute;
		right: 130rpx;
		top: 0;
		margin-right: 0;
	}

	.upload {
		position: absolute;
		right: 65rpx;
		top: 0;
		margin-right: 0;
	}

	.rights {
		position: absolute;
		/* display: inline-block; */
		right: 0;
		top: 0;
		margin-right: 0;
		/* width: 100%; */

	}

	.cu-bar {
		min-height: 80px;
	}

	.cu-btn {
		font-size: 16px;
		height: 40px;
	}

	.foot {
		background: #fff;
	}

	.cu-item {
		flex-wrap: wrap;
		padding: 8px 15px !important;
	}

	.title {
		width: 220rpx;
	}

	.cu-modal-slot {
		margin: 42rpx 32rpx 20rpx 32rpx;
		height: 100vh;
		position: relative;
	}

	.cu-modal-header {
		font-size: 38rpx;
		color: #3d3d3d;
		line-height: 40rpx;
		text-align: left;
		font-weight: 500;
	}

	.button {
		font-size: 32rpx;
		color: #666666;
		line-height: 40rpx;
		padding: 12rpx 40rpx;
		margin-bottom: 20rpx;
		background: #e2e2e2;
		border-radius: 180rpx;
	}

	.button:hover {
		background: #3e97b0;
		color: #ffffff;
	}

	.cu-modal-search-title {
		margin: 32rpx 0rpx 20rpx 0rpx;
		text-align: left;
		font-size: 32rpx;
		color: #999999;
		line-height: 40rpx;
	}

	.cu-dialog {
		background: #ffffff;
	}

	.cu-modal-footer {
		padding: 32rpx 32rpx !important;
		width: 100%;

		.cu-btn {
			width: 50%;
		}
	}

	.bg-confirm {
		background: #3e97b0;
		color: #ffffff;
	}
</style>